package service

import (
	"net/http"

	"github.com/gookit/color"
	"github.com/gorilla/websocket"
)

var (
	WebsocketService = websocketService{}
	upgrader         = websocket.Upgrader{
		ReadBufferSize:  1024,
		WriteBufferSize: 1024,
	}
)

type websocketService struct{}

func (websocketSrv *websocketService) HandleWebsocket(w http.ResponseWriter, r *http.Request) {
	conn, err := upgrader.Upgrade(w, r, nil)
	if err != nil {
		color.Red.Println(err)
		return
	}

	for {
		messageType, p, err := conn.ReadMessage()
		if err != nil {
			color.Red.Println(err)
			return
		}

		if err := conn.WriteMessage(messageType, p); err != nil {
			color.Red.Println(err)
			return
		}
	}
}
